package marking.liu.config;

import lombok.extern.slf4j.Slf4j;
import marking.liu.properties.TaskThreadPoolInfoProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableConfigurationProperties({TaskThreadPoolInfoProperties.class})
@Slf4j
public class TaskThreadPoolConfig {

    @Autowired
    private TaskThreadPoolInfoProperties taskThreadPoolInfoProperties;

    @Bean(destroyMethod = "shutdown")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor () {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        设置核心线程数
        executor.setCorePoolSize(taskThreadPoolInfoProperties.getCorePoolSize());
//        设置最大线程数
        executor.setMaxPoolSize(taskThreadPoolInfoProperties.getMaxPoolSize());
//        设置空闲线程最大存货时间
        executor.setKeepAliveSeconds(taskThreadPoolInfoProperties.getKeepAliveSeconds());
//        设置任务队列长度
        executor.setQueueCapacity(taskThreadPoolInfoProperties.getQueueCapacity());
//        设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
//        将设置参数初始化
        executor.initialize();
        return executor;
    }
}
